from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

from LaboratoryBuilding.settings import PHONE_AK, PHONE_SECRET, PHONE_REGION_ID
from apis.celery_pro.celery import app

@app.task
def send_phone(code1,phone):
    client = AcsClient(ak=PHONE_AK, secret=PHONE_SECRET, region_id=PHONE_REGION_ID)
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', "%s"%phone)
    request.add_query_param('SignName', "美朵朵的主页")
    request.add_query_param('TemplateCode', "SMS_195585843")
    request.add_query_param('TemplateParam', "{\"code\":\"%d\"}"%code1)

    response = client.do_action(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))
